﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using WPFDemoMVVM.Services;
using WPFDemoMVVM.View;
using WPFDemoMVVM.ViewModel;

namespace WPFDemoMVVM
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public new static App Current => (App)Application.Current;
        public IServiceProvider Services { get; set; }
        public App()
        {
            Services = ConfigureServices();

            var culture = new CultureInfo("en");
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
        }

        public IServiceProvider ConfigureServices()
        {
            var services = new ServiceCollection();
            services.AddSingleton<MainWindowViewModel>();
            services.AddSingleton<MainWindow>(sp=> new WPFDemoMVVM.MainWindow { DataContext =sp.GetService<MainWindowViewModel>()});
            services.AddSingleton<RadioButtonViewModel>();
            services.AddSingleton<RadioButtonView>(sp => new RadioButtonView { DataContext = sp.GetService<RadioButtonViewModel>() });
            services.AddSingleton<BehaviorViewModel>();
            services.AddSingleton<BehaviorView>(sp => new BehaviorView { DataContext = sp.GetService<BehaviorViewModel>() });
            services.AddSingleton<ValueConverterViewModel>();
            services.AddSingleton<ValueConverterView>(sp => new ValueConverterView { DataContext = sp.GetService<ValueConverterViewModel>() });
            services.AddSingleton<ItemControlViewModel>();
            services.AddSingleton<ItemControlView>(sp => new ItemControlView { DataContext = sp.GetService<ItemControlViewModel>() });
            services.AddSingleton<ItemControlSortViewModel>();
            services.AddSingleton<ItemControlSortView>(sp => new ItemControlSortView { DataContext = sp.GetService<ItemControlSortViewModel>() });
            services.AddSingleton<DataGridViewModel>();
            services.AddSingleton<DataGridView>(sp => new DataGridView { DataContext = sp.GetService<DataGridViewModel>() });
            services.AddSingleton<DataGridAndPageViewModel>();
            services.AddSingleton<DataGridAndPageView>(sp => new DataGridAndPageView { DataContext = sp.GetService<DataGridAndPageViewModel>() });

            services.AddSingleton<LanguageChangeViewModel>();
            services.AddSingleton<LanguageChangeView>(sp => new LanguageChangeView { DataContext = sp.GetService<LanguageChangeViewModel>() });

            services.AddSingleton<ComBoxBindNumberViewModel>();
            services.AddSingleton<ComBoxBindNumberView>(sp=>new ComBoxBindNumberView { DataContext = sp.GetService<ComBoxBindNumberViewModel>() });

            services.AddSingleton<UserInputViewModel>();
            services.AddSingleton<UserInputView>(sp => new UserInputView { DataContext = sp.GetService<UserInputViewModel>() });


            //services.AddSingleton<ParentWindowViewModel>();
            //services.AddSingleton<ParentWindow>(sp => new ParentWindow { DataContext = sp.GetService<ParentWindowViewModel>() });


            services.AddSingleton<IMessageBoxService, MessageBoxService>();
            services.AddSingleton<IUserInputService, UserInputService>();


            return services.BuildServiceProvider();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            var mainView = Services.GetService<MainWindow>();
            mainView.Show();
        }
    }
}
